<template>
	<view class="content">
		<view class="addman-box">
			<u--form labelPosition="left" :model="form" :rules="rules" ref="uForm" labelWidth="80"
				:labelStyle="labelStyle">
				<u-form-item label="住客姓名" prop="personName" borderBottom>
					<u--input v-model="form.personName" border="none"></u--input>
				</u-form-item>
				<u-form-item label="身份证号" prop="idCard" borderBottom>
					<u--input v-model="form.idCard" border="none"></u--input>
				</u-form-item>
				<u-form-item label="联系手机" prop="personPhone" borderBottom>
					<u--input v-model="form.personPhone" border="none"></u--input>
				</u-form-item>
				<u-form-item label="设为默认" prop="defaultState" borderBottom>
					<u-switch v-model="form.defaultState" :size="15" inactiveValue="1" activeValue="0"
						activeColor="#19B84E" @change="change"></u-switch>
				</u-form-item>
			</u--form>
			<view class="agreement">
				<view class="flex align-center title">
					<image class="safety-img"
						src="https://saas-v5.oss-cn-guangzhou.aliyuncs.com/saas-admin/druid/mpimg/safety.png" mode="">
					</image>隐私保护
				</view>
        <ul class="custom-list">
          <li>1. 根据国家相关法律法规，入住经营性质的酒店、民宿，必须提供入住者的身份信息。</li>
          <li>2. 您的身份信息平台会严格保密，仅用于必要的身份核实。</li>
          <li>3. 上述手机号码仅在房东提供入住与相关服务时使用。</li>
        </ul>
			</view>
			<button class="save-btn" @click="savePerson">保存</button>
			<button class="del-btn" @click="delPerson" v-if="personId">删除</button>
		</view>
	</view>
</template>

<script>
	import {
		personList,
		addPerson,
		putPerson,
		getPerson,
		delPerson
	} from '@/api/home';
	const getSex = (no = '') => {
		if (!no) return no
		const toStr = no.toString()
		if (toStr.length < 17) return no
		return Number(toStr.substring(16, 17)) % 2 ? '1' : '2' //1男 2女
	}
	export default {
		data() {
			return {
				labelStyle: {
					"font-weight": 500,
					"font-size": "30rpx",
					color: "#666666"
				},
				selectIndex: 0,
				show: false,
				form: {
					"personName": "", //入住人姓名
					"personPhone": "", //入住人手机号码
					"idCard": "", //入住人身份证号码
					"sex": "", //性别：1男 2女
					"defaultState": "1" //是否默认（0默认,1非默认）
				},
				rules: {
					personName: [{
						required: true,
						message: '请输入住客姓名',
						trigger: ['change', 'blur'],
					}, ],
					idCard: [{
						required: true,
						message: '请输入身份证号',
						trigger: ['change', 'blur'],
					}, ],
					// 字段名称
					personPhone: [{
							required: true,
							message: '请输入住客手机号',
							trigger: ['change', 'blur'],
						},
						{
							// 自定义验证函数，见上说明
							validator: (rule, value, callback) => {
								// 上面有说，返回true表示校验通过，返回false表示不通过
								// uni.$u.test.mobile()就是返回true或者false的
								return uni.$u.test.mobile(value);
							},
							message: '手机号格式错误',
							// 触发器可以同时用blur和change
							trigger: ['change', 'blur'],
						}
					]
				},
				list: [],
				personId: ''
			}

		},

		onLoad(opt) {
			this.personId = opt.personId
			if (opt.personId) {
				this.getData();
			}
		},
		onReady() {
			//如果需要兼容微信小程序，并且校验规则中含有方法等，只能通过setRules方法设置规则。
			this.$refs.uForm.setRules(this.rules)
		},
		methods: {
			delPerson() {
				uni.showModal({
					title: '提示',
					content: '确认删除入住人？',
					confirmColor: '#FF7947',
					success: res => {
						if (res.confirm) {
							delPerson(this.personId).then(res => {
								uni.showToast({
									title: '删除成功'
								})
								setTimeout(() => {
									uni.navigateBack()
								}, 1500)
							})

						}
					}
				});
			},
			selectHandel(index, state) {
				if (this.type == 'lessee') {
					this.list.map(item => {
						item.select = false;
					})
					this.list[index].select = true;
				} else {
					this.list[index].select = !state;
				}

			},
			getData() {
				getPerson(this.personId).then(res => {
					this.form = res.data;
				})
			},
			change() {

			},
			addHandel() {
				this.form = this.$options.data().form;
				this.show = true;
			},
			savePerson() {
				this.$refs.uForm.validate().then(res => {
					if (this.form.personId) {
						this.putPerson()
					} else {
						this.addPerson()
					}
				}).catch(err => {})

			},
			putHandel(row) {
				this.form = row;
				this.show = true;
			},
			addPerson() {
				this.form.sex = getSex(this.form.idCard)
				addPerson(this.form).then(res => {
					uni.showToast({
						title: "新增成功"
					})
					setTimeout(() => {
						uni.navigateBack()
					}, 1500)

				})
			},
			putPerson() {
				this.form.sex = getSex(this.form.idCard)
				putPerson(this.form).then(res => {
					uni.showToast({
						title: "编辑成功"
					})
					setTimeout(() => {
						uni.navigateBack()
					}, 1500)
				})
			},
			close() {
				this.show = false;
			},
			open() {

			},
			submit() {
				let result = this.list.filter(item => item.select === true)
				if (result.length < this.length) {
					uni.showToast({
						title: '已次添加人数大于可入住人数',
						icon: 'none'
					})
					return
				}
				this.$emit('submit', result)
			}
		}
	}
</script>

<style lang="scss">
	page {
		background-color: #FFFFFF !important;
	}

	.content {
		padding-top: 30rpx;
	}

	.pop-title {
		font-weight: 800;
		font-size: 34rpx;
		color: #000000;
		text-align: center;
		font-style: normal;
		text-transform: none;
		margin: 40rpx 0 46rpx 0;
	}

	.item-box {
		width: 696rpx;
		height: 172rpx;
		margin-left: 28rpx;
		border-bottom: 1px solid rgba(112, 112, 112, 0.1);
		padding-bottom: 36rpx;
		background: #FFFFFF;
		border-radius: 16rpx 16rpx 16rpx 16rpx;
		margin-bottom: 20rpx;
		padding: 30rpx;

		.select-img {
			width: 40rpx;
			height: 40rpx;
			margin-right: 36rpx;
		}

		.edit {
			font-weight: bold;
			font-size: 22rpx;
			color: #4967D6;
			text-align: left;
			font-style: normal;
			text-transform: none;
		}

		.name {
			font-weight: bold;
			font-size: 32rpx;
			color: #111111;
			text-align: left;
			font-style: normal;
			text-transform: none;
			margin-right: 16rpx;
		}

		.default {
			width: 76rpx;
			height: 36rpx;
			background: #EEEEEE;
			border-radius: 6rpx 6rpx 6rpx 6rpx;
			font-weight: 400;
			font-size: 22rpx;
			line-height: 36rpx;
			color: #111111;
			text-align: center;
			font-style: normal;
			text-transform: none;
		}
	}

	.tip-text {
		line-height: 34rpx;
		font-weight: 500;
		font-size: 24rpx;
		color: #666666;
		text-align: left;
		font-style: normal;
		text-transform: none;
	}

	.bottom-btn {
		position: fixed;
		bottom: 0;
		left: 0;
		width: 690rpx;
		height: 88rpx;
		background: #FF7947;
		border-radius: 20rpx 20rpx 20rpx 20rpx;
		font-weight: 500;
		font-size: 30rpx;
		color: #FFFFFF;
		line-height: 88rpx;
		text-align: center;
		font-style: normal;
		text-transform: none;
		margin-left: 30rpx;
		margin-bottom: constant(safe-area-inset-bottom); // 兼容 IOS<11.2
		margin-bottom: env(safe-area-inset-bottom); // 兼容 IOS>11.2
	}

	.add-btn {
		width: 690rpx;
		height: 94rpx;
		background: rgba(255, 121, 71, 0.1);
		border-radius: 20rpx 20rpx 20rpx 20rpx;
		font-weight: 800;
		font-size: 30rpx;
		color: #FF7947;
		line-height: 94rpx;
		text-align: center;
		font-style: normal;
		text-transform: none;
		margin-bottom: 52rpx;
	}

	.addman-box {
		width: 750rpx;
		//height: 906rpx;
    min-height: 906rpx;
    max-height: 100vh;
		padding: 20rpx 30rpx;

	}

	.save-btn {
		width: 690rpx;
		height: 88rpx;
		background: #FF7947;
		border-radius: 20rpx 20rpx 20rpx 20rpx;
		font-size: 30rpx;
		color: #FFFFFF;
		line-height: 88rpx;
		text-align: center;
	}

	.del-btn {
		width: 690rpx;
		height: 94rpx;
		border-radius: 20rpx 20rpx 20rpx 20rpx;
		border: 2rpx solid #FF7947;
		font-weight: 800;
		font-size: 30rpx;
		color: #FF7947;
		line-height: 94rpx;
		text-align: center;
		margin-top: 36rpx;
	}

	.agreement {
		width: 690rpx;
		height: 252rpx;
		background: #F7F8FA;
		border-radius: 20rpx 20rpx 20rpx 20rpx;
		padding: 28rpx 24rpx;
		font-size: 22rpx;
		color: #9B9B9B;
		text-align: left;
		margin-top: 42rpx;
		margin-bottom: 70rpx;

		.title {
			font-weight: bold;
			font-size: 24rpx;
			color: #111111;
			text-align: left;
			margin-bottom: 16rpx;
		}

		.safety-img {
			width: 20rpx;
			height: 22rpx;
			margin-right: 6rpx;
		}


  }
</style>
